<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange" label="退药日期">
          <a-range-picker
            v-model:value="formState.dateRange"
            class="w-64"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            :allow-clear="false"
            @change="getData()"
          />
        </a-form-item>
        <a-form-item name="zjm" label="" class="ml-4">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="药品编号/药品名称/助记码" allow-clear @keydown.enter="getData" />
        </a-form-item>
        <a-form-item name="sbzt" label="" class="ml-4">
          <!--sbzt 0未上报 1上报 2删除 -->
          <a-radio-group v-model:value="formState.sbzt" @change="getData()">
            <a-radio value="">全部</a-radio>
            <a-radio :value="0">未上报</a-radio>
            <a-radio :value="1">已上报</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="" label="" class="ml-4">
          <a-divider type="vertical" style="height: 24px; background-color: #999" />
        </a-form-item>
        <!--ybjslx 0：自费 1:医保 不传全部 -->
        <a-form-item name="ybjslx" label="" class="ml-4">
          <a-radio-group v-model:value="formState.ybjslx" @change="getData()">
            <a-radio value="">全部</a-radio>
            <a-radio value="1">医保</a-radio>
            <a-radio value="0">自费</a-radio>
          </a-radio-group>
        </a-form-item>
        <!--xsysc 0不显示 1显示 未传显示 -->
        <a-form-item name="xsysc" label="显示已删除" class="ml-4 flex items-center">
          <a-switch v-model:checked="formState.xsysc" @change="getData()" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-end">
        <BaseExport type="医保" name="西成药退药" code="000765" :search-params="{ ...formState, xsysc: formState.xsysc ? 1 : 0 }">导出</BaseExport>
        <!-- <a-button class="mb-2" :loading="signInLoading" @click="onExport"> 导出 </a-button> -->
        <a-button class="mb-2 ml-2" type="primary" :loading="signOutLoading" @click="onReport(1)"> 全部上报 </a-button>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" row-key="id" column-code="000765" :pagination="pagination" :scroll="{ x: 7000 }">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'xsthsj'">
            <EditCell title="销售日期">
              <a-date-picker
                v-model:value="record.xsthsj"
                :show-time="{ format: 'HH:mm:ss' }"
                placeholder="请选择"
                value-format="YYYY-MM-DD HH:mm:ss"
                @blur="onSaveRow(record)"
              />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'xsthsl'">
            <EditCell title="销售数量">
              <BaseInput v-model:value="record.xsthsl" class="text-right" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'ylmlbm'">
            <EditCell title="医疗目录编码">
              <BaseInput v-model:value="record.ylmlbm" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'ddyyjgpclsh'">
            <EditCell title="批次流水号">
              <BaseInput v-model:value="record.ddyyjgpclsh" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'ysxm'">
            <EditCell title="医师姓名">
              <BaseInput v-model:value="record.ysxm" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'yszyzgzh'">
            <EditCell title="药师资格证号">
              <BaseInput v-model:value="record.yszyzgzh" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'ybfyjslxmc'">
            <EditCell title="医保费用结算类型">
              <a-select v-model:value="record.ybfyjslx" placeholder="请选择" @change="onSaveRow(record)">
                <a-select-option :value="'0'">非医保结算</a-select-option>
                <a-select-option :value="'1'">医保结算</a-select-option>
              </a-select>
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'jylsh'">
            <EditCell title="就医流水号">
              <BaseInput v-model:value="record.jylsh" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'scph'">
            <EditCell title="生产批号">
              <BaseInput v-model:value="record.scph" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'scrq'">
            <EditCell title="生产日期">
              <a-date-picker v-model:value="record.scrq" placeholder="请选择" value-format="YYYY-MM-DD" @blur="onSaveRow(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'cfybzmc'">
            <EditCell title="处方药标志">
              <a-select v-model:value="record.cfybz" placeholder="请选择" @change="onSaveRow(record)">
                <a-select-option :value="'0'">否</a-select-option>
                <a-select-option :value="'1'">是</a-select-option>
              </a-select>
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'clbzmc'">
            <EditCell title="拆零标志名称">
              <a-select v-model:value="record.clbz" placeholder="请选择" @change="onSaveRow(record)">
                <a-select-option :value="'0'">否</a-select-option>
                <a-select-option :value="'1'">是</a-select-option>
              </a-select>
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'lsdjh'">
            <EditCell title="零售单据号">
              <BaseInput v-model:value="record.lsdjh" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'pch'">
            <EditCell title="批次号">
              <BaseInput v-model:value="record.pch" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" />
            </EditCell>
          </template>
          <template v-if="column.dataIndex === 'xsthjbrxm'">
            <EditCell title="销售员">
              <SelectTable v-model:value="record.xsthjbrxm" type="员工" @select="onSelect(record, $event)" />
              <!-- <BaseInput v-model:value="record.xsthjbrxm" placeholder="请输入" @change="record.change = true" @blur="onBlurInput(record)" /> -->
            </EditCell>
          </template>

          <template v-if="column.dataIndex === 'ypmc'">
            <MedicineInfo :value="{ mc: record.ypmc, gg: record.ypgg, cj: record.ypcj }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button v-if="record.sbzt === 0" type="link" size="small" @click="onReport(record)">上报</a-button>
            <a-button v-if="record.sbzt === 1" type="link" size="small" @click="onCancelReport(record)">撤销上报</a-button>
            <a-button v-if="record.sbzt === 2" type="link" danger size="small" @click="onCancelDelete(record)">恢复</a-button>
            <a-button v-else type="link" danger size="small" @click="onDelete(record)">删除</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  zjm: '',
  sbzt: '',
  ybjslx: '',
  xsysc: true
})
const signOutLoading = ref(false)
const ipMac = ref({
  ip: '',
  mac: ''
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/insureReconciliation/getWesternMedicineReturnList',
  immediate: true,
  params: formState.value,
  formatParams: (params) => {
    return {
      ...params,
      stDate: formState.value.dateRange[0],
      edDate: formState.value.dateRange[1],
      xsysc: params.xsysc ? 1 : 0
    }
  }
})

const onBlurInput = (record) => {
  if (record.change) {
    onSaveRow(record)
    record.change = false
  }
}
const onSelect = (record, even) => {
  record.xsthjbr = even.empId
  record.xsthjbrxm = even.empName
  onSaveRow(record)
}
const onSaveRow = async (row) => {
  const { code, msg } = await post('/insureReconciliation/doUpdateWesternMedicineReturn', {
    ...row
  })
  if (code !== 0) return message.error(msg || '修改失败')
  message.success(msg || '操作成功')
  getData()
}

const onReport = async (row) => {
  let recordIdList = []
  if (row === 1) {
    signOutLoading.value = true
    recordIdList = tableData.value.map((item) => {
      return item.id
    })
  } else {
    recordIdList = [row.id]
  }
  const { code, msg } = await post('/insureReconciliation/doUploadWesternMedicineReturn', {
    recordIdList
  })
  signOutLoading.value = false
  if (code !== 0) return message.error(msg || '操作失败')
  getData()
}
const onDelete = async (row) => {
  const { code, msg } = await post('/insureReconciliation/doDelWesternMedicineReturn', {
    id: row.id
  })
  if (code !== 0) return message.error(msg || '操作失败')
  getData()
}
const onCancelDelete = async (row) => {
  const { code, msg } = await post('/insureReconciliation/doRecoverDeletedWesternMedicineReturn', {
    id: row.id
  })
  if (code !== 0) return message.error(msg || '操作失败')
  getData()
}
const onCancelReport = async (row) => {
  const { code, msg } = await post('/insureReconciliation/doCancelUploadWesternMedicineReturn', {
    id: row.id
  })
  if (code !== 0) return message.error(msg || '操作失败')
  getData()
}

onMounted(() => {
  if (window.openExe?.getIPAndMAC()) {
    ipMac.value = JSON.parse(window.openExe?.getIPAndMAC()) || {}
  }
})
</script>
<style></style>
